package org.flowers.unbeaten.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @description 自定义Redis序列化方式
 * @author RyanWang
 * @date 2021-07-16 16:34:59
 * @version 1.0
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        // StringRedisSerializer 支持string序列化方式
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // CustomStringRedisSerializer 支持object序列化方式
        CustomStringRedisSerializer customStringRedisSerializer = new CustomStringRedisSerializer();
        // 设置键的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // 设置值的序列化方式
        template.setValueSerializer(customStringRedisSerializer);
        // 设置hash键的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // 设置hash值的序列化方式
        template.setHashValueSerializer(customStringRedisSerializer);
        return template;
    }

}
